package com.yc.navy.blog.blog.listenter;

import com.yc.navy.blog.blog.util.ConstantInfo;
import com.yc.navy.blog.blog.util.StringUtil;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.io.IOException;

public class CreateUploadPathListenter implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        String path=sce.getServletContext().getInitParameter("uploadPath");
        if (!StringUtil.checknull(path)){
            ConstantInfo.uploadPath=path;
        }
        String basepath=sce.getServletContext().getRealPath("/");//到了当前项目下
        File parent=new File(basepath).getParentFile();//tomcat的webapps目录
        File fl=new File(parent,ConstantInfo.uploadPath);
        if (!fl.exists()){
            fl.mkdirs();
        }try {
            ConstantInfo.basepath=parent.getCanonicalPath();

        }catch(IOException e){
            throw new RuntimeException(e);
        }
    }
}
